<?php
namespace app\common\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use think\Log;
use app\common\request\Curl;
use app\common\request\ClientLai;
use app\common\model\CarsInfo;

class CarsDetail extends Command
{
	protected function configure()
    {
        $this->setName('carsdetail')->setDescription('cars details');
    }

    protected function execute(Input $input, Output $output)
    {
        
        self::weiCars();
        self::bolutu();
        self::iotBolutu();
            
    	$output->writeln('success');
    }

    public static function bolutu(){
    	Db::name('cars_info')->where('supplier_no',1376920002)->field('car_unique_id')->chunk(100,function($cars){
	    	$data['car_unique_id_arr'] = array_column($cars,'car_unique_id');
	    	$cars = implode(',',$data['car_unique_id_arr']);
	        $config = config('gz_blt');
	        $obj = new ClientLai($config['user_name'],$config['password']);
	        $res = $obj->request_post(rtrim($config['url'],'/').'/api/vehicle/carData/getRtInfo',http_build_query(['car_unique_id'=>$cars]));
	        $arr = json_decode($res,true);
	        if($arr['result'] == 0){
	        	$cars_data = $arr['data'];
	        	foreach ($cars_data as &$value) {
	        		$value['long_term_lease'] = $value['supplier_rent_type'];
	        	}
	        	try {
                    $model = new CarsInfo;
                    $model->allowField(true)->saveAll($cars_data);
        			
                } catch (\Exception $e) {
                    Log::record('[更新cars_info表S车辆数据]'.$e->getMessage(),'error');
                    return false;
                }
                return true;
	        }	
        });	
    }

    public static function iotBolutu(){
    	Db::name('cars_info')->where('supplier_no',1376920003)->field('car_unique_id')->chunk(100,function($cars){
    		$data['car_unique_id_arr'] = array_column($cars,'car_unique_id');
	    	$cars = implode(',',$data['car_unique_id_arr']);
	    	$config = config('local_iot');
	        $obj = new ClientLai($config['user_name'],$config['password']);
	        $res = $obj->request_post(rtrim($config['url'],'/').'/car/getRtInfo',http_build_query(['car_unique_id'=>$cars]));
	        $arr = json_decode($res,true);
	       	if($arr['result'] == 0){
                // Log::record('[请求回来的数据]'.json_encode($arr['data']),'log');
                $cars_data = array_filter($arr['data']);
                foreach ($cars_data as $key => $value) {
                    if($value['car_unique_id'] !== null){
                        try {
                            $model = new CarsInfo;
                            $model->allowField(true)->save($value,['car_unique_id' =>$value['car_unique_id']]);
                            
                        } catch (\Exception $e) {
                            Log::record('[更新cars_info表S-IOT车辆数据]'.$e->getMessage(),'error');
                            return false;
                        }
                   }
                }
                
                return true;
	        }	
    	});

    }

    public static function weiCars(){
    	Db::name('cars_info')->where('supplier_no',1376920001)->field('car_unique_id')->chunk(100,function($cars){
    		$data['car_unique_id_arr'] = array_column($cars,'car_unique_id');
            $curl = new Curl;
            $header = config('car_hear.header');
            $url = config('car_hear.url');
            $res = $curl->set('CURLOPT_HTTPHEADER',$header)->post($data)->url($url);
            if ($curl->error()) {
                Log::record($curl->message(),'error');
            }else{
                    // 任务进程信息
                $info = $curl->info();               
                if($info['http_code'] == 500){ 
                    Log::record('对方服务器错误','error');  
                    exit();
                }else{
                    // 任务结果内容
                    $content = $curl->data();
                }
            }
            $arr = json_decode($content,true);
            if($arr['result'] == 0){
                /*$cars_data = $arr['data'];
                foreach ($cars_data as &$value) {
                    unset($value['supplier_no'],$value['buy_time'],$value['type'],$value['license_plate'],$value['mileage_max'],$value['main_picture'],$value['bluetooth_pwd'],$value['equipment_id'],$value['bluetooth_id'],$value['brand'],$value['model'],$value['car_engine_code'],$value['max_electricity'],$value['brand_id_model_id'],$value['data_status'],$value['update_time'],$value['obd_status'],$value['gprs_signal'],$value['gps_signal'],$value['positioning_validity']);
                }*/
                try {
                    $model = new CarsInfo;
                    $model->allowField(true)->saveAll($arr['data']);
                    
                } catch (\Exception $e) {
                    Log::record('[更新cars_info表W车辆数据]'.$e->getMessage(),'error');
                    return false;
                }
                return true;

            }    

        });
           
    }
}